package com.symaster.common.geom.canvas.listener;

import com.symaster.common.geom.Canvas;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

/**
 * @author yinmiao
 * @version 2022-07-14 14:50
 */
public class CanvasKeyListener implements KeyListener {

    private final Canvas canvas;

    public CanvasKeyListener(Canvas canvas) {
        this.canvas = canvas;
    }

    @Override
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void keyPressed(KeyEvent e) {
        if (17 == e.getKeyCode()) {
            canvas.setCtrlPressed(true);
        }
        if (38 == e.getKeyCode()) {
            canvas.moveSelectAll(0, -1);
        }
        if (40 == e.getKeyCode()) {
            canvas.moveSelectAll(0, 1);
        }
        if (37 == e.getKeyCode()) {
            canvas.moveSelectAll(-1, 0);
        }
        if (39 == e.getKeyCode()) {
            canvas.moveSelectAll(1, 0);
        }
        if (90 == e.getKeyCode() && canvas.isCtrlPressed()) {
            canvas.rollback();
        }
        if (67 == e.getKeyCode() && canvas.isCtrlPressed()) {
            canvas.copyListener();
        }
        if (86 == e.getKeyCode() && canvas.isCtrlPressed()) {
            canvas.pasteListener();
        }
        canvas.mouseReleased(null);
    }

    @Override
    public void keyReleased(KeyEvent e) {
        if (65 == e.getKeyCode() && canvas.isCtrlPressed()) {
            canvas.selectAll();
        }
        if (17 == e.getKeyCode()) {
            canvas.setCtrlPressed(false);
        }
    }
}
